﻿@{
    Layout = null;
    var objPrimaryKey = Model.ColumnInfos.First();
    var objExceptPrimaryKey = Model.ColumnInfos.Except(new List<ColumnInfo> { objPrimaryKey }).ToList();
    string entityName = Model.Name;
}@inherits  BuildCodeViewPage<TableInfo>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.CMS.Process;
using TjZheng.Controls;
using TjZheng.Framework;
using @(Model.Namespace).Business;
using @(Model.Namespace).DataEntity;

namespace TjZheng.CMS.Controllers
{
    public class @(entityName)Controller : CMSController
    {
        /// <summary>
        /// 业务逻辑层
        /// </summary>
        Biz@(entityName) objBiz@(entityName) { get; set; }
        public @(entityName)Controller()
        {
            objBiz@(entityName) = new Biz@(entityName)();
        }
        /// <summary>
        /// 列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 列表数据或搜索数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">页条数</param>
        /// <returns></returns>
        [QueryMsSqlFilter]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            return Json(objBiz@(entityName).GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "@objPrimaryKey.Name.ToFirstUpper()"));
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id">数据id</param>
        /// <returns></returns>
        public ActionResult EditPage(@CurrentBuilder.GetNoticeType(objPrimaryKey.DataType,objPrimaryKey.IsNull)? id)
        {
            @(entityName) obj@(entityName) = null;
            if (id.HasValue)
            {
                obj@(entityName) = objBiz@(entityName).GetRecord(id.Value);
            }
            else
            {
                obj@(entityName) = new @(entityName)();
            }
            return View(obj@(entityName));
        }
        /// <summary>
        /// 编辑页面-保存
        /// </summary>
        /// <param name="objProvide">数据源</param>
        /// <returns></returns>
        //[ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Save(@(entityName) objProvide)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "要保存的数据不存在";
                return Json(objInvokeResult, "text/plain");
            }
            @(entityName) objTarget = null;
            if (objProvide.@objPrimaryKey.Name <= 0)
            {
                objTarget = new @(entityName)();
                Save(objTarget, objProvide);
                objBiz@(entityName).Add(objTarget);
            }
            else
            {
                objTarget = objBiz@(entityName).GetRecord(objProvide.@objPrimaryKey.Name);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "TargetNull";
                    objInvokeResult.ResultMessage = "数据源不存在";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBiz@(entityName).Update(objTarget);
            }

            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }
        /// <summary>
        /// 列表页面-删除
        /// </summary>
        /// <param name="id">数据id或id集合</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBiz@(entityName).DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }
        /// <summary>
        /// 数据赋值
        /// </summary>
        /// <param name="objTarget">目标数据</param>
        /// <param name="objProvide">数据源</param>
        void Save(@(entityName) objTarget, @(entityName) objProvide)
        {
    @foreach (var objColumnInfo in objExceptPrimaryKey)
    {
            <tj>//@objColumnInfo.Description</tj>
        if (CurrentBuilder.IsString(objColumnInfo.DataType.ToLower()))
        {
            <tj>objTarget.@objColumnInfo.Name = objProvide.@(objColumnInfo.Name).CutWord(@(objColumnInfo.Datalength), @Raw(objColumnInfo.IsNull ? "null" : "\"\""));</tj>
        }
        else if (objColumnInfo.DataType == "text")
        {
            <tj>objTarget.@objColumnInfo.Name = objProvide.@(objColumnInfo.Name).SetNullToDefaultValue(@Raw(objColumnInfo.IsNull ? "null" : "\"\""));</tj>
        }
        else
        {
            <tj>objTarget.@objColumnInfo.Name = objProvide.@objColumnInfo.Name;</tj>
        }
    }
        }
    }
}